<template>
  <div class="announcements">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>公告列表</span>
        </div>
      </template>
      
      <div class="announcements-list">
        <div 
          v-for="announcement in announcements" 
          :key="announcement.title"
          class="announcement-card"
          @click="showDetail(announcement)"
        >
          <div class="announcement-header">
            <h3>{{ announcement.title }}</h3>
            <el-tag :type="announcement.type === '通知' ? 'primary' : 'warning'">
              {{ announcement.type }}
            </el-tag>
          </div>
          <div class="announcement-content">
            {{ announcement.content.substring(0, 200) }}...
          </div>
          <div class="announcement-footer">
            <span class="author">发布人：{{ announcement.author }}</span>
            <span class="time">{{ formatDate(announcement.time) }}</span>
          </div>
        </div>
        
        <div v-if="announcements.length === 0" class="empty-state">
          <el-empty description="暂无公告" />
        </div>
      </div>
    </el-card>
    
    <!-- 详情弹窗 -->
    <el-dialog
      v-model="detailVisible"
      :title="currentAnnouncement.title"
      width="70%"
    >
      <div class="detail-content">
        <div class="detail-meta">
          <el-tag :type="currentAnnouncement.type === '通知' ? 'primary' : 'warning'">
            {{ currentAnnouncement.type }}
          </el-tag>
          <span class="detail-author">发布人：{{ currentAnnouncement.author }}</span>
          <span class="detail-time">{{ formatDate(currentAnnouncement.time) }}</span>
        </div>
        <div class="detail-text" v-html="currentAnnouncement.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '../../utils/request'

const announcements = ref([])
const detailVisible = ref(false)
const currentAnnouncement = ref({})

const formatDate = (dateString) => {
  return new Date(dateString).toLocaleString()
}

const loadAnnouncements = async () => {
  try {
    const response = await request.get('/api/board/list')
    if (response.data.code === 200) {
      announcements.value = response.data.data
    }
  } catch (error) {
    console.error('加载公告失败:', error)
  }
}

const showDetail = (announcement) => {
  currentAnnouncement.value = announcement
  detailVisible.value = true
}

onMounted(() => {
  loadAnnouncements()
})
</script>

<style scoped>
.announcements {
  padding: 20px;
}

.announcements-list {
  max-height: 70vh;
  overflow-y: auto;
}

.announcement-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 15px;
  cursor: pointer;
  transition: all 0.3s;
}

.announcement-card:hover {
  border-color: #409EFF;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.announcement-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.announcement-header h3 {
  margin: 0;
  color: #303133;
  font-size: 18px;
}

.announcement-content {
  color: #606266;
  line-height: 1.6;
  margin-bottom: 15px;
}

.announcement-footer {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #909399;
}

.detail-content {
  max-height: 60vh;
  overflow-y: auto;
}

.detail-meta {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e4e7ed;
}

.detail-author,
.detail-time {
  margin-left: 15px;
  color: #909399;
  font-size: 14px;
}

.detail-text {
  line-height: 1.8;
  color: #303133;
  white-space: pre-wrap;
}
</style> 